layui.use(['element', 'layer', 'form', 'table'], function () {
    var element = layui.element;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;

    form.on('submit(loginSubmitBtn)', function(data){
        $.ajax({
            url: '/front/login/loginData',
            type: 'post',
            data: data.field,
            dataType: 'json',
            success: function (e) {
                layer.msg(e.msg);
                if (e.msg == "登陆成功") {
                    setTimeout(function () {
                        window.location.href="/front/index"
                    },1000);
                }
            }
        });
        return false;
    });

    $('#regsterBtn').click(function () {
        var content = $('#registerView');
        layer.open({
            type: 1,
            skin: 'layui-layer-molv',
            title: ['新用户注册', 'font-size:18px;'],
            closeBtn: 1,
            shade: 0.3,
            content: content,
            area: ['70%', '80%'],
            shadeClose: true,
            resize: true,
            anim: 1,
            id: 'LAY_layuiroregisterUser', //设定一个id，防止重复弹出
            moveType: 1,
            maxmin: false,
            success: function (layero, index) {
                $('id').val('');
                $('name').val('');
                $('age').val('');
                $('[name="sex"][value="男"]').prop('checked', true);
                $('phone').val('');
                $('mail').val('');
                $('idCard').val('');
                $('address').val('');
                $('password').val('');
                form.on('submit(registerNewUserBtn)', function(data){
                    var params = $.param({
                        mydata: JSON.stringify(data.field)
                    }, true);
                    $.ajax({
                        url: '/front/register/registerData',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (e) {
                            layer.msg(e.msg);
                            if (e.msg == "注册成功") {
                                layer.close(index);
                            }
                        }
                    });
                    return false;
                });
            },
            cancel: function (index, layero) {
                $('id').val('');
                $('name').val('');
                $('age').val('');
                $('[name="sex"][value="男"]').prop('checked', true);
                $('phone').val('');
                $('mail').val('');
                $('idCard').val('');
                $('address').val('');
                $('password').val('');
            }
        });
    });

    $('#forgetPwd').click(function () {
        var content = $('#forgetPwdView');
        layer.open({
            type: 1,
            skin: 'layui-layer-molv',
            title: ['找回密码', 'font-size:18px;'],
            closeBtn: 1,
            shade: 0.3,
            content: content,
            area: ['50%', '50%'],
            shadeClose: true,
            resize: true,
            anim: 1,
            id: 'LAY_layuirofindPwd', //设定一个id，防止重复弹出
            moveType: 1,
            maxmin: false,
            success: function (layero, index) {
                $('#forgetphone').val('');
                $('#forgetidCard').val('');
                $('#forgetpassword').val('');
                $('#updateInfo').click(function () {
                    var phone = $('#forgetphone').val();
                    var idCard = $('#forgetidCard').val();
                    var password = $('#forgetpassword').val();
                    if (phone === '' || idCard === '' || password ==='') {
                        layer.msg("请输入完整信息");
                        return false;
                    }
                    var params = $.param({
                        phone:phone,
                        idCard:idCard,
                        password:password
                    }, true);
                    $.ajax({
                        url: '/front/forgetPwd',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (e) {
                            layer.msg(e.msg);
                            if (e.msg == "密码已成功找回，请登录") {
                                layer.close(index);
                            }
                        }
                    });
                })
            },
            cancel: function (index, layero) {
                $('#forgetphone').val('');
                $('#forgetidCard').val('');
                $('#forgetpassword').val('');
            }
        })
    });

});